/*
 * @lc app=leetcode.cn id=622 lang=java
 *
 * [622] 设计循环队列
 */

// @lc code=start
class MyCircularQueue {

    int[] queueArr;
    int frontIndex = 0;
    int endIndex = 0;//最后一个位置是空的，它的前一个是有元素的
    int count = 0;

    public MyCircularQueue(int k) {
        queueArr = new int[k];
    }
    
    public boolean enQueue(int value) {
        if(isFull()) return false;
        
        queueArr[endIndex] = value;

        endIndex ++;
        if(endIndex > queueArr.length-1){
            endIndex = endIndex % queueArr.length;
        }

        // System.out.println("value :: "+value+" endIndex :: "+endIndex);

        count++;
        return true;
    }
    
    public boolean deQueue() {
        if(isEmpty()) return false;
        count--; 

        frontIndex++;
        if(frontIndex > (queueArr.length-1)){
            frontIndex = frontIndex % queueArr.length;
        }
        return true;
    }
    
    public int Front() {
        if(count==0) return -1;
        return queueArr[frontIndex];
    }
    
    public int Rear() {
        // System.out.println(Arrays.toString(queueArr) +"\t"+endIndex);
        if(count==0) return -1;
        int endCur = endIndex - 1;
        if(endCur == -1){
            endCur = queueArr.length-1;
        }
        return queueArr[endCur];
    }
    
    public boolean isEmpty() {
        return count == 0;
    }
    
    public boolean isFull() {
        return count == queueArr.length;
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */
// @lc code=end

